import {
    fetchLockers as apiFetchLockers,
    deleteLocker as apiDeleteLocker,
    addLocker as apiAddLocker,
    updateLocker as apiUpdateLocker,
} from '../services/api'; // 引入 API

const locker = {
    state() {
        return {
            lockers: [], // 存储快递柜数据
        };
    },
    mutations: {
        SET_LOCKERS(state, lockers) {
            state.lockers = lockers;
        },
    },
    actions: {
        fetchLockers({ commit }) {
            return apiFetchLockers().then(response => {
                commit('SET_LOCKERS', response.data);
            });
        },
        deleteLocker({ dispatch }, lockerId) {
            return apiDeleteLocker(lockerId).then(() => {
                return dispatch('fetchLockers'); // 刷新快递柜列表
            });
        },
        addLocker({ dispatch }, lockerData) {
            return apiAddLocker(lockerData).then(() => {
                return dispatch('fetchLockers'); // 刷新快递柜列表
            });
        },
        updateLocker({ dispatch }, lockerData) {
            return apiUpdateLocker(lockerData).then(() => {
                return dispatch('fetchLockers'); // 刷新快递柜列表
            });
        },
    },
    getters: {
        lockers: (state) => state.lockers,
    },
};

export default locker;
